package com.idrive.activity;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

import com.idrive.R;
import com.idrive.database.DatabaseUtility;
import com.idrive.database.dao.CallLogDao;
import com.idrive.database.dao.CommonDataDao;
import com.idrive.database.dto.CallLog;
import com.idrive.database.dto.CommonData;

public class SettingsActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);

		final CommonDataDao dao = new CommonDataDao(this);
		CommonData data = dao.getCommonData();

		String option = data.getOption();

		if (DatabaseUtility.SETTINGS_OPTION_SPEAKER_PHONE.equals(option)) {
			RadioButton radioButton = (RadioButton) findViewById(R.id.speakerPhoneRadioButton);
			radioButton.setChecked(Boolean.TRUE);
		} else if (DatabaseUtility.SETTINGS_OPTION_SMS.equals(option)) {
			RadioButton radioButton = (RadioButton) findViewById(R.id.smsRadioButton);
			radioButton.setChecked(Boolean.TRUE);
		} else if (DatabaseUtility.SETTINGS_OPTION_VOICE_MESSAGE.equals(option)) {
			RadioButton radioButton = (RadioButton) findViewById(R.id.voiceRadioButton);
			radioButton.setChecked(Boolean.TRUE);
		} else if (DatabaseUtility.SETTINGS_OPTION_REJECT_CALL.equals(option)) {
			RadioButton radioButton = (RadioButton) findViewById(R.id.rejectRadioButton);
			radioButton.setChecked(Boolean.TRUE);
		}

		final Button button = (Button) findViewById(R.id.okCallManagerSettingsButton);
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				final RadioButton option1 = (RadioButton) findViewById(R.id.speakerPhoneRadioButton);
				final RadioButton option2 = (RadioButton) findViewById(R.id.smsRadioButton);
				final RadioButton option3 = (RadioButton) findViewById(R.id.voiceRadioButton);
				final RadioButton option4 = (RadioButton) findViewById(R.id.rejectRadioButton);

				if (option1.isChecked()) {
					CommonData data = new CommonData();
					data.setOption(DatabaseUtility.SETTINGS_OPTION_SPEAKER_PHONE);
					dao.updateCommonData(data);
					Toast.makeText(SettingsActivity.this,
					        "Call will be automatically answered and speakerphone activated on an incoming call.",
					        Toast.LENGTH_LONG).show();
					// setContentView(R.layout.activity_call_manager);
					Intent myIntent = new Intent(SettingsActivity.this, CallManagerActivity.class);
					SettingsActivity.this.startActivity(myIntent);

				} else if (option2.isChecked()) {
					// setContentView(R.layout.activity_sms);
					Intent myIntent = new Intent(SettingsActivity.this, SMSActivity.class);
					SettingsActivity.this.startActivity(myIntent);
				} else if (option3.isChecked()) {
					CommonData data = new CommonData();
					data.setOption(DatabaseUtility.SETTINGS_OPTION_VOICE_MESSAGE);
					dao.updateCommonData(data);
					// setContentView(R.layout.activity_voice);
					Intent myIntent = new Intent(SettingsActivity.this, VoiceMessageActivity.class);
					SettingsActivity.this.startActivity(myIntent);
				} else if (option4.isChecked()) {
					CommonData data = new CommonData();
					data.setOption(DatabaseUtility.SETTINGS_OPTION_REJECT_CALL);
					dao.updateCommonData(data);
					Toast.makeText(SettingsActivity.this,
					        "Call will be automatically rejected and details wil be stored to view later.",
					        Toast.LENGTH_LONG).show();
					// setContentView(R.layout.activity_call_manager);
					Intent myIntent = new Intent(SettingsActivity.this, CallManagerActivity.class);
					SettingsActivity.this.startActivity(myIntent);

				}
			}
		});
	}

}
